package BVRClient;

public class BVRC_Line
{
	public enum Clockness
	{
		CLOCKWISE, COUNTER_CLOCKWISE, LINE
	}
	
	public BVRC_Point p1 = new BVRC_Point(0, 0);
	public BVRC_Point p2 = new BVRC_Point(1, 1);
	
	/**
	 * 
	 */
	public BVRC_Line() {}
	
	/**
	 * 
	 * @param p1x
	 * @param p1y
	 * @param p2x
	 * @param p2y
	 */
	public BVRC_Line(int p1x, int p1y, int p2x, int p2y)
	{
		this.p1 = new BVRC_Point(p1x, p1y);
		this.p2 = new BVRC_Point(p2x, p2y);
	}
	
	/**
	 * 
	 * @param p1x
	 * @param p1y
	 * @param p2x
	 * @param p2y
	 */
	public BVRC_Line(float p1x, float p1y, float p2x, float p2y)
	{
		this.p1 = new BVRC_Point(p1x, p1y);
		this.p2 = new BVRC_Point(p2x, p2y);
	}
	
	/**
	 * 
	 * @param p1
	 * @param p2
	 */
	public BVRC_Line(BVRC_Point p1, BVRC_Point p2)
	{
		this.p1 = p1;
		this.p2 = p2;
	}

	/**
	 * 
	 * @param l
	 * @return true if the line intersects this line, false in all other cases
	 */
	public boolean lineIntersects(BVRC_Line l)
	{
		Clockness test1_a;
		Clockness test1_b;
		Clockness test2_a;
		Clockness test2_b;

		test1_a = checkTriClockDir(this.p1, this.p2, l.p1);
		test1_b = checkTriClockDir(this.p1, this.p2, l.p2);
		
		if (test1_a != test1_b)
		{
			test2_a = checkTriClockDir(l.p1, l.p2, this.p1);
			test2_b = checkTriClockDir(l.p1, l.p2, this.p2);
			
			if (test2_a != test2_b)
			{
				return true;
			}
		}
		
		return false;
	}
	
	/**
	 * 
	 * @param p1
	 * @param p2
	 * @param p3
	 * @return the clockness of the triangle
	 */
	private Clockness checkTriClockDir(BVRC_Point p1, BVRC_Point p2, BVRC_Point p3)
	{
		float test = (((p2.x - p1.x) * (p3.y - p1.y)) - ((p3.x - p1.x) * (p2.y - p1.y)));
		
		if (test > 0)
			return Clockness.COUNTER_CLOCKWISE;
		else if (test < 0)
			return Clockness.CLOCKWISE;
		else
			return Clockness.LINE;
	}
}
